package walasp.proto;

import java.util.ArrayList;
import java.util.Date;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

/**
 * 
 * A custom ArrayAdapter used to generate custom list values specific to the
 * purpose of this Application
 * 
 */
public class TaskAdapter extends ArrayAdapter<Task> {

	ArrayList<Task> tasks;
	Context con;

	/**
	 * Creates the Custom List Adapter which displays both Task name and date
	 * for a single item based on a ArrayList<Task>
	 * 
	 * @param context
	 *            the Context
	 * @param textViewResourceId
	 *            the textViewResourceId
	 * @param objects
	 *            the objects
	 * @return the new TaskAdapter instance
	 */
	public TaskAdapter(Context context, int textViewResourceId,
			ArrayList<Task> objects) {
		super(context, textViewResourceId, objects);
		this.tasks = objects;
		con = context;

	}

	/**
	 * Method called by a ListActivity to display a item on a ListView
	 * 
	 * @param position
	 * @param ContentView
	 * @param ParentView
	 * @return the View
	 */
	public View getView(int position, View ContentView, ViewGroup ParentView) {

		View working = ContentView;// the view to be created/modified
		if (!tasks.isEmpty()) {
			// check to see if any items exist
			if (working == null) {
				// check to see if THIS item already exists, if it doesn't use
				// and inflater to generate a new view based on the single Task
				// Row layout otherwise just update it
				LayoutInflater inflator = (LayoutInflater) con
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				working = inflator.inflate(R.layout.taskrow, null);
			}
			//get the two text views to have their values set.
			TextView name = (TextView) working.findViewById(R.id.TaskNameView);
			TextView due = (TextView) working.findViewById(R.id.DueDateView);
			
			
			//shorten the name if its longer than 15 chars 
			String nameworker = tasks.get(position).getTaskName();
			if (nameworker.length() > 15) {
				name.setText(nameworker.substring(0, 16) + "...");
			} else {
				name.setText(nameworker);
			}

			//check the status of the task, if its a current task display the date otherwise display overdue/pending/finished
			if (tasks.get(position).getStatus().equals(Status.PENDING)) {
				due.setText(Status.PENDING);
			} else if (tasks.get(position).getStatus().equals(Status.FINISHED)) {
				due.setText(Status.FINISHED);
			} else if (tasks.get(position).getStatus().equals(Status.CANCELLED)) {
				due.setText(Status.CANCELLED);
			} else {
				//we are displaying the date so do some formatting
				Date date = tasks.get(position).getDueDate();
				if (date.getTime() == 0) {
					//oh no theres no due date! better make it nothing
					due.setText(" ");
					due.setVisibility(0);
				} else {
					//ah ha, there is this time better make it nice
					String dateStr = date.toLocaleString();
					String[] parts = dateStr.split(" ");
					if (parts.length < 5) {
						due.setText(dateStr);
						return working;
					}
					// strip seconds off, because that's way too precise
					parts[3] = parts[3].substring(0, parts[3].length() - 3);
					long current = new Date().getTime();
					if (date.getTime() - current >= 86400000) {
						due.setText(parts[0] + " " + parts[1] + " " + parts[2]);
					} else {
						if (date.getTime() - current < 0) {
							due.setText("Overdue");
						} else {
							due.setText(parts[3] + parts[4]);
						}
					}
				}
			}
		}
		return working;
	}

}
